<template>
  <div class="cesium-draw">
    <div id="cesium-drawer" />
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div class="tool-box">
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="terrainExaggeration" :max="2" /></div> -->
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="relativeHeight" :max="10000" /></div> -->
      <div class="select-box-wrapper"><select-box :options="locationToolbarOptions" /></div>
      <div class="select-box-wrapper"><select-box :options="entityToolbarOptions" /></div>
      <div class="select-box-wrapper"><select-box :options="sizeOptions" /></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="shadows">shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="softShadows">soft shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="terrainShadows">terrain shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="entityShadows">entity shadows</a-checkbox></div>
      <div id="menu">
        <p><button @click="onClick('play')">播放</button></p>
        <p><button @click="onClick('stop')">暂停</button></p>
      </div>
    </div>
  </div>
</template>

<script>
//参考：Cesium深入浅出之图层管理器 https://blog.csdn.net/fywindmoon/article/details/113747843
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import SliderBox from '@components/widgets/slider-box';
import SelectBox from '@components/widgets/select-box';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';

export default defineComponent({
  name: 'shadows', // 阴影
  components: { DownOutlined, EditOutlined, SliderBox, SelectBox },
  setup() {
    return {
      viewer: null,
      setEntityShadows: () => {}
    };
  },
  data() {
    return {
      nowPostion: {},
      shadows: true,
      softShadows: true,
      terrainShadows: true,
      entityShadows: true,
      locationToolbarOptions: [],
      entityToolbarOptions: [],
      sizeOptions: []
    };
  },
  computed: {},
  watch: {
    shadows(val) {
      this.viewer && (this.viewer.shadows = val);
    },
    softShadows(val) {
      this.viewer && (this.viewer.shadowMap.softShadows = val);
    },
    terrainShadows(val) {
      const terrainShadows = val ? Cesium.ShadowMode.ENABLED : Cesium.ShadowMode.DISABLED;
      this.viewer && (this.viewer.terrainShadows = terrainShadows);
    },
    entityShadows(val) {
      this.setEntityShadows(val);
    }
  },
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    initEffects() {
      const viewer = this.viewer;

      var scene = viewer.scene;
      var globe = scene.globe;

      scene.screenSpaceCameraController.enableCollisionDetection = false;

      var longitude = -3.82518;
      var latitude = 53.11728;
      var height = -500.0;
      var position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height);
      var url = '//zzgis.com/cdn/3d/SampleData/models/ParcLeadMine/ParcLeadMine.glb';

      var entity = viewer.entities.add({
        name: url,
        position: position,
        model: {
          uri: url
        }
      });
      
      return;

      viewer.scene.camera.setView({
        destination: new Cesium.Cartesian3(3827058.651471591, -256575.7981065622, 5078738.238484612),
        orientation: new Cesium.HeadingPitchRoll(1.9765540737339418, -0.17352018581162754, 0.0030147639151465455),
        endTransform: Cesium.Matrix4.IDENTITY
      });

      var originalColor = Cesium.Color.BLACK;
      var originalNearDistance = 1000.0;
      var originalFarDistance = 1000000.0;
      var originalNearAlpha = 0.0;
      var originalFarAlpha = 1.0;

      var color = originalColor;

      var viewModel = {
        enabled: true,
        nearDistance: originalNearDistance,
        farDistance: originalFarDistance,
        nearAlpha: originalNearAlpha,
        farAlpha: originalFarAlpha
      };

      // Cesium.knockout.track(viewModel);
      // var toolbar = document.getElementById('toolbar');
      // Cesium.knockout.applyBindings(viewModel, toolbar);
      // for (var name in viewModel) {
      //   if (viewModel.hasOwnProperty(name)) {
      //     Cesium.knockout.getObservable(viewModel, name).subscribe(update);
      //   }
      // }

      // Sandcastle.addToolbarButton('Random color', function() {
      //   color = Cesium.Color.fromRandom({
      //     alpha: 1.0
      //   });
      //   update();
      // });

      // Sandcastle.addToolbarButton('Clear', function() {
      //   color = originalColor;
      //   viewModel.enabled = true;
      //   viewModel.nearDistance = originalNearDistance;
      //   viewModel.farDistance = originalFarDistance;
      //   viewModel.nearAlpha = originalNearAlpha;
      //   viewModel.farAlpha = originalFarAlpha;
      //   update();
      // });

      function update() {
        globe.undergroundColor = viewModel.enabled ? color : undefined;

        var nearDistance = Number(viewModel.nearDistance);
        nearDistance = isNaN(nearDistance) ? originalNearDistance : nearDistance;

        var farDistance = Number(viewModel.farDistance);
        farDistance = isNaN(farDistance) ? originalFarDistance : farDistance;

        if (nearDistance > farDistance) {
          nearDistance = farDistance;
        }

        var nearAlpha = Number(viewModel.nearAlpha);
        nearAlpha = isNaN(nearAlpha) ? 0.0 : nearAlpha;

        var farAlpha = Number(viewModel.farAlpha);
        farAlpha = isNaN(farAlpha) ? 1.0 : farAlpha;

        globe.undergroundColorAlphaByDistance.near = nearDistance;
        globe.undergroundColorAlphaByDistance.far = farDistance;
        globe.undergroundColorAlphaByDistance.nearValue = nearAlpha;
        globe.undergroundColorAlphaByDistance.farValue = farAlpha;
      }
      // update();
    },

    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },

    initViewer() {
      let vm = this;
      Cesium.Ion.defaultAccessToken = $accessToken.cs;

      let osm = new Cesium.OpenStreetMapImageryProvider({
        url: 'https://a.tile.openstreetmap.org/'
      });
      let viewer = new Cesium.Viewer('cesium-drawer', {
        // imageryProvider: osm,
        terrainProvider: Cesium.createWorldTerrain(),
        contextOptions: {
          webgl: {
            alpha: true
          }
        },
        selectionIndicator: false,
        // shadows: true,
        // terrainShadows: Cesium.ShadowMode.ENABLED,
        animation: false, //是否显示动画控件
        baseLayerPicker: false, //是否显示图层选择控件
        geocoder: false, //是否显示地名查找控件
        // timeline: false, //是否显示时间线控件
        sceneModePicker: false, //是否显示投影方式控件
        navigationHelpButton: false, //是否显示帮助信息控件
        infoBox: false, //是否显示点击要素之后显示的信息
        fullscreenButton: false,
        shouldAnimate: true
      });

      // viewer.scene.globe.depthTestAgainstTerrain = true;

      this.viewer = viewer;
      this.onViewerInited(viewer);

      this.initEffects();

      // this.viewer.flyTo(this.viewer.entities);
    },

    onClick(value) {
      switch (value) {
        case 'play':
          this.stratPlay();
          break;
        case 'stop':
          this.stopPlay();
          break;
      }
    },

    stratPlay() {
      const viewer = this.viewer;
      if (((viewer.clock.shouldAnimate = !0), this.stopTime)) {
        viewer.clock.currentTime = this.stopTime;
      } else {
        // var e = '2019-06-18';
        const t = new Date(),
          i = '00',
          a = '24',
          r = new Date(new Date(t).setHours(Number(i))),
          o = new Date(new Date(t).setHours(Number(a)));
        (viewer.scene.globe.enableLighting = !0),
          (viewer.shadows = !0),
          (viewer.clock.startTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.currentTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.stopTime = Cesium.JulianDate.fromDate(o)),
          (viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP),
          (viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER),
          (viewer.clock.multiplier = 1600);
      }
    },

    stopPlay() {
      const viewer = this.viewer;
      (this.stopTime = viewer.clock.currentTime), (viewer.clock.shouldAnimate = !1);
    }
  }
});
</script>

<style lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    .cesium-viewer-bottom {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }
}

.chart-container {
  position: absolute;
  bottom: 7px;
  left: 7px;
  background-color: rgba(0, 0, 0, 0.5);

  #profileChart {
    width: 600px;
    height: 300px;
  }
}
#canvas-a {
  top: 10px;
  display: none;
}

#canvas-b {
  top: 120px;
  display: none;
}

.tool-box {
  position: absolute;
  top: 70px;
  left: 10px;
  z-index: 9;

  #menu {
    padding-top: 20px;
    button {
      background-color: rgba(0, 0, 0, 0.75);
      border-color: rgba(0, 0, 0, 0);
      cursor: pointer;
      transition: all 500ms;
      &:hover {
        opacity: 0.75;
      }
    }
  }

  .slider-box-wrapper {
    width: 18em;
    padding: 0 10px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
  }

  .select-box-wrapper {
    padding: 5px 15px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
    width: auto;
  }
}
</style>
